package com.mnxiaotu.controller;

import com.github.pagehelper.util.StringUtil;
import com.mnxiaotu.entity.User;
import com.mnxiaotu.service.UserService;
import com.mnxiaotu.common.vo.Result;
import com.mnxiaotu.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86182
 */
@CrossOrigin
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/list")
    // @PreAuthorize("hasRole('ROLE_admin2')")
//    @PreAuthorize("hasAuthority('system:user2:list')")
    public Result userList(@RequestHeader(required = false) String token){
//        List<User> userList = userService.list();
        if(StringUtil.isNotEmpty(token)){
            Map<String,Object> resutMap=new HashMap<>();
            List<User> userList = userService.list();
            resutMap.put("userList",userList);
            return Result.ok(resutMap);
        }else{
            return Result.error(401,"没有权限访问");
        }
    }

    @GetMapping("/login")
    public Result login(){
        String token= JwtUtils.genJwtToken("java1234");
        return Result.ok().put("token",token);
    }
}
